﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NegevGas.Core;
using WebUI.Controls;
using System.IO;
using OSGeo.MapGuide;
using System.Collections.Specialized;

namespace NegevGas.Extensions.FeatureCard
{
    public partial class FeatureEditor : System.Web.UI.Page
    {
        private FeatPropertyCollection fpCollection;
        private HtzMgHelper _helper;
        private const string _moduleName = "FeatureCard";
        private bool _refresh;
        //protected FeatureEditorControl featureEditorControl;
        //protected TabContainer FeatureTabs;
        //protected TabPanel FilesTab;
        //protected UploaderControl FileUploadControl;
        //protected Panel FileUploaderDisabled;
        //protected Label lblLayerLegend;
        //protected MaintenanceControl maintenanceControl;
        //protected Panel MaintenanceDisabledPanel;
        //protected Panel MaintenanceHolder;
        //protected TabPanel MaintenanceTab;
        //protected TabPanel PropertiesTab;
        //protected Literal scriptHole;

        private void ForceRefresh(object sender, EventArgs e)
        {
            if (!this._refresh)
            {
                this.scriptHole.Text = "<script type='text/javascript'>parent.parent.mapFrame.Refresh();</script>";
                this._refresh = true;
            }
        }

        protected void OnControlError(object sender, ErrorEventArgs e)
        {
            // this.Master.ShowErrorMessage(e.ErrorMessage);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
          //  this.maintenanceControl.RefreshRequired += new EventHandler(this.ForceRefresh);
            this.featureEditorControl.RefreshRequired += new EventHandler(this.ForceRefresh);
            //this.UploaderControl.R.RefreshRequired += new EventHandler(this.ForceRefresh);
        }
        public NameValueCollection GetRequestParameters()
        {
            if ("POST" == HttpContext.Current.Request.HttpMethod)
            {
                return HttpContext.Current.Request.Form;
            }
            else
            {
                return HttpContext.Current.Request.QueryString;
            }
        }
        bool iseditmode = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            int mgProjectId;
            

            if (!base.IsPostBack)
            {
                this.ViewState.Add("SESSION", base.Request["SESSION"]);
                this.ViewState.Add("MAPNAME", base.Request["MAPNAME"]);
                this.ViewState.Add("LAYOUT", base.Request["LAYOUT"]);
                this.ViewState.Add("UIDKEY", string.IsNullOrEmpty(base.Request["UIDKEY"]) ? "FeatId" : base.Request["UIDKEY"]);
                if (!int.TryParse(GetRequestParameters()["mgProjectId"], out mgProjectId)) return;
                ViewState.Add("mgProjectId", mgProjectId);
                this.ViewState.Add("ProjectName", base.Request["ProjectName"]);

                  this.FileUploadControl.IdField ="FeatId";
                            this.ViewState.Add("UIDKEY","FeatId");
                            this.ViewState.Add("IdField", "FeatId");

              
            }
            else
            {
                if (Session["SESSION"] == null)
                {
                    Session["SESSION"] = ViewState["SESSION"];
                }
                mgProjectId = (int)ViewState["mgProjectId"];
                
            }
           // this._config = new LayoutModulesConfiguration(this.ViewState["LAYOUT"].ToString());

            SiteUtilities siteUtilities = new SiteUtilities(new MapSettings(mgProjectId));
            // get properties from the xml 

         //   ModuleConfig configProperties = this._config.GetProperties("FeatureCard");
            //if (configProperties.Count == 0)
            //{
            //    DefineControlProperties.Define("FeatureCard");
            //    configProperties = this._config.GetProperties("FeatureCard");
            //}
            this._helper = new HtzMgHelper(this.ViewState["SESSION"].ToString(), this.ViewState["MAPNAME"].ToString(), this.ViewState["LAYOUT"].ToString());
            _helper._projectname = this.ViewState["ProjectName"].ToString();
           // fpCollection = new FeatPropertyCollection(siteUtilities, ViewState["LayerName"].ToString(), ViewState["Filter"].ToString());
            // this.Master.ErrorVisible = false;
            if (!base.IsPostBack)
            {
                if (this._helper.Selection.GetLayers() == null)
                {
                    // this.Master.ShowErrorMessage("לא נבחר האובייקט");
                    return;
                }
                this.ViewState.Add("LayerName", this._helper.Selection.GetLayers().GetItem(0).GetName());
            }

            MgLayer layer = (MgLayer)this._helper.Map.GetLayers().GetItem(this.ViewState["LayerName"].ToString());
            if (!base.IsPostBack)
            {
                string filter = this._helper.Selection.GenerateFilter(layer, layer.GetFeatureClassName());
                if (filter == "")
                {
                    base.Response.Write("האופציה הזו לא קיימת לפרית הזאת.");
                    base.Response.End();
                }
                this.ViewState.Add("Filter", filter);
            }
            this.featureEditorControl.Filter = this.ViewState["Filter"].ToString();
            try
            {
                Feature feature = Feature.GetSingleFeature(this._helper, this.ViewState["Filter"].ToString(), this.ViewState["LayerName"].ToString(), this.ViewState["UIDKEY"].ToString());
                this.featureEditorControl.Activate(this._helper, feature);
               
                    
               
              //  this.featureEditorControl.Activate(this._helper, feature);
                //((bool)configProperties["MaintenanceAllowed"].Value) ||
                //if (( feature.Layer.GetFeatureSourceId().Contains("Markup")) && feature.HaveUID)
                //{
                //    this.maintenanceControl.Activate(this._helper, this.ViewState["UIDKEY"].ToString(), feature.UID, feature.Layer);
                //    if (this.maintenanceControl.Activated)
                //    {
                //        this.MaintenanceDisabledPanel.Visible = false;
                //        this.maintenanceControl.Visible = true;
                //    }
                //    else
                //    {
                //        this.MaintenanceDisabledPanel.Visible = true;
                //        this.maintenanceControl.Visible = false;
                //    }
                //}
                //else
                //{
                //    this.MaintenanceDisabledPanel.Visible = true;
                //    this.maintenanceControl.Visible = false;
                //}
                //this.MaintenanceDisabledPanel.Visible = false;
                //this.maintenanceControl.Visible = false;
               // this.FileUploadControl.Activate(this._helper, configProperties);
                // GetConfiguration From XML  
                this.FileUploadControl.Visible = true;
                    //(bool)configProperties["FileUploadAllowed"].Value;
                //!((bool)configProperties["FileUploadAllowed"].Value);
                this.FileUploaderDisabled.Visible= false;
            }
            catch (Exception ex)
            {
                //   this.Master.ShowErrorMessage(ex.Message);
            }
            this.FileUploadControl.ControlError += new EventHandler<ErrorEventArgs>(this.OnControlError);
            this.lblLayerLegend.Text = this.featureEditorControl.LayerLegend;
            MgLayerGroup group = layer.GetGroup();
            this.LayerLegend = layer.GetLegendLabel();
            while (group != null)
            {
                this.LayerLegend = string.Format("{0} > {1}", group.GetLegendLabel(), this.LayerLegend);
                group = group.GetGroup();
            }
            this.lblLayerLegend.Text = this.LayerLegend;
        }

        public string LayerLegend { get; set; }

       
    }
}